// 提取URL的基础部分（协议+域名+端口）
function getBaseUrl(url) {
    try {
        const parsedUrl = new URL(url);
        return `${parsedUrl.protocol}//${parsedUrl.host}`;
    } catch (e) {
        return null;
    }
}




// 监控fetch请求
function interceptFetch() {
    console.log('fetch--11',);
    const originalFetch = window.fetch;
    window.fetch = async function (input, init) {
        let url = typeof input === 'string' ? input : input.url;
        const baseUrl = getBaseUrl(url);

        // 只处理非当前页面域名的API请求
        if (baseUrl && !url.includes(window.location.host) && baseUrl !== getBaseUrl(window.location.href)) {
            // 发送检测到的API基础地址给background
            chrome.runtime.sendMessage({
                type: 'apiDetected',
                originUrl: baseUrl
            });
            console.log('fetch--', baseUrl);
        }

        return originalFetch.apply(this, arguments);
    };
}
// 监控XMLHttpRequest请求
function interceptXHR() {
    console.log('axios--xhr11',);
    const originalXhrOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function (method, url) {
        const baseUrl = getBaseUrl(url);
        // 只处理非当前页面域名的API请求
        if (baseUrl && !url.includes(window.location.host) && baseUrl !== getBaseUrl(window.location.href)) {
            // 发送检测到的API基础地址给background
            chrome.runtime.sendMessage({
                type: 'apiDetected',
                originUrl: baseUrl
            });
            console.log('axios--xhr', baseUrl);

        }

        return originalXhrOpen.apply(this, arguments);
    };
}
//在接口异常监控的时候也适用
// 1. 监控fetch请求
interceptFetch()
// 2. 监控XMLHttpRequest请求
interceptXHR()